﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

namespace Tester2
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建5个工位
            var workstations = new List<Workstation>();
            for (int i = 1; i <= 5; i++)
            {
                workstations.Add(new Workstation(i));
            }

            // 模拟设备信号
            var random = new Random();
            while (true)
            {
                foreach (var workstation in workstations)
                {
                    if (workstation.IsIdle)
                    {
                        // 如果工位空闲，从Buffer获取配件
                        var part = Buffer.GetPart();
                        if (part != null)
                        {
                            var stopwatch = new Stopwatch();
                            stopwatch.Start();

                            workstation.ProcessPart(part);

                            stopwatch.Stop();
                            Console.WriteLine($"Task{workstation.WorkstationNumber} used {stopwatch.ElapsedMilliseconds} ms");
                        }
                    }
                }

                // 模拟一段时间后再次检查
                Thread.Sleep(1000);
            }
        }
    }

    class Part
    {
        public int PartNumber { get; set; }
    }

    static class Buffer
    {
        private static Queue<Part> parts = new Queue<Part>();

        public static Part GetPart()
        {
            // 从Buffer获取配件
            return parts.Count > 0 ? parts.Dequeue() : null;
        }
    }

    class Workstation
    {
        public int WorkstationNumber { get; }
        public bool IsIdle { get; private set; }

        public Workstation(int workstationNumber)
        {
            WorkstationNumber = workstationNumber;
            IsIdle = true;
        }

        public void ProcessPart(Part part)
        {
            // 模拟处理配件的操作
            Console.WriteLine($"Task{WorkstationNumber} is processing Part{part.PartNumber}");
            IsIdle = false;

            // 模拟处理时间
            Thread.Sleep(2000);

            // 处理完成后释放工位
            IsIdle = true;
        }
    }
}
